package com.blue.waffle.iwc.utils;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;

public class PreferencesManager {

	private static final String PREF_NAME="IWC";
	private static final String PREF_SOUND_VOLUME="sound.volume";
	private static final String PREF_MUSIC_VOLUME="music.volume";
	private static final String PREF_SOUND_ACTIVE="sound.active";
	private static final String PREF_MUSIC_ACTIVE="music.active";
	
	private static final String PREF_DB_VERSION="db.version";
	
	public Preferences getGamePreferences(){
		return Gdx.app.getPreferences(PREF_NAME);
	}
	
	public Preferences getPreferences(String name){
		return Gdx.app.getPreferences(name);
	}
	
	public void setSoundVolume(float volume){
		setValue(PREF_SOUND_VOLUME, volume);
	}
	
	public void setMusicVolume(float volume){
		setValue(PREF_MUSIC_VOLUME, volume);
	}
	
	public void setSoundActive(boolean active){
		setValue(PREF_SOUND_ACTIVE, active);
	}
	
	public void setMusicActive(boolean active) {
		setValue(PREF_MUSIC_ACTIVE, active);
	}
	
	public void setDbVersion(int dbVersion){
		setValue(PREF_DB_VERSION, dbVersion);
	}
	
	public int getDbVersion(){
		return getGamePreferences().getInteger(PREF_DB_VERSION, 0);
	}
	
	public void setValue(String key, Object value){
		Preferences preferences=getGamePreferences();
		if (value instanceof Boolean) {
			preferences.putBoolean(key, (Boolean)value);
		}
		else if (value instanceof Float) {
			preferences.putFloat(key, (Float)value);
		}else if (value instanceof Integer) {
			preferences.putInteger(key, (Integer)value);
		}else if (value instanceof Long) {
			preferences.putLong(key, (Long)value);
		}else if (value instanceof String) {
			preferences.putString(key, (String)value);
		}
		preferences.flush();
	}

	public float getSoundVolume(){
		return getGamePreferences().getFloat(PREF_SOUND_VOLUME, 0.7f);
	}
	
	public float getMusicVolume(){
		return getGamePreferences().getFloat(PREF_MUSIC_VOLUME, 0.7f);
	}
	
	public boolean isSoundActive(){
		return getGamePreferences().getBoolean(PREF_SOUND_ACTIVE,true);
	}
	
	public boolean isMusicActive(){
		return getGamePreferences().getBoolean(PREF_MUSIC_ACTIVE,true);
	}
}
